package com.qf.quduba.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.wxpay.sdk.WXPay;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.config.MyWxPayConfig;
import com.qf.quduba.entity.Recharge;
import com.qf.quduba.entity.User;
import com.qf.quduba.service.UserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 17312
 * @time2020/10/1020:51
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("login")
    public ResultVO login(String username, String password){
        try {
            User user = userService.loginByUsernameAndPassword(username, password);
            if(user != null){
                //UUID生成唯一的key
                String key = UUID.randomUUID().toString().replace("-", "");
                //将对象转成json字符串
                String jsonStr = new ObjectMapper().writeValueAsString(user);
                //将信息保存到redis并设置过期时间
                Boolean aBoolean = stringRedisTemplate.boundValueOps(key).setIfAbsent(jsonStr, 30, TimeUnit.MINUTES);
                return new ResultVO(1000,key,user);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("register")
    public ResultVO registerUser(@RequestBody User user){
        int i = userService.registerUser(user);
        if (i>0){
            return new ResultVO(1000,"成功",user);
        }else {
            return new ResultVO(1002,"失败",null);
        }
    }

    @GetMapping("reSelect")
    public ResultVO reSelect(Integer userId){
        User user = userService.selectUser(userId);
        if (user != null){
            return new ResultVO(1000,"成功",user);
        }else {
            return new ResultVO(1002,"失败",null);
        }
    }
    @PostMapping("updateUser")
    public ResultVO updateUser(@RequestBody User user){
        int i = userService.updateByPrimaryKeySelective(user);
        if (i>0){
            return new ResultVO(1000,"成功",user);
        }else {
            return new ResultVO(1002,"失败",null);
        }
    }

    @PostMapping("addAuthor")
    public ResultVO addAuthor(@RequestBody User user){
        int i = userService.addAuthor(user);
        if (i>0){
            return new ResultVO(1000,"成功",user);
        }else {
            return new ResultVO(1002,"失败",null);
        }
    }

    //修改用户余额
    @PutMapping("updateMoney")
    //
    public ResultVO updateMoney(Double consumeAmount,Integer userId){
        User user = userService.selectUser(userId);
        double newMoney = user.getMoney()-consumeAmount;
        int i = userService.updateUserMoney(userId,newMoney);
        if (i>0){
            return new ResultVO(1000,"成功",null);
        }else {
            return new ResultVO(1002,"失败",null);
        }
    }

}
